package com.hao.cdk.sm.core.mabatisplus;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

/**
 * @author: 许自豪
 * @date: 2025-01-23 17:16:01
 * @description: MybatisPlus自动配置类
 */

@Slf4j
@EnableTransactionManagement(
        order = 2048
)
@Configuration
@MapperScan({"com.hao.**.mapper", "com.hao.**.dao"})
@ComponentScan({"com.hao.cdk.sm.core.mabatisplus"})
public class MybatisPlusAutoConfiguration {
    @Autowired
    private PaginationProperties paginationProperties;

    public MybatisPlusAutoConfiguration() {
        log.debug("加载MybatisPlus配置...");
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() throws Exception {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(this.paginationProperties.getDbType());
        paginationInnerInterceptor.setMaxLimit(this.paginationProperties.getMaxLimit());
        paginationInnerInterceptor.setOverflow(this.paginationProperties.isOverflow());
        if (!StringUtils.isEmpty(this.paginationProperties.getDialectClassName())) {
            Class<?> dialectClass = Class.forName(this.paginationProperties.getDialectClassName());
            Object dialectInstance = dialectClass.newInstance();
            if (!(dialectInstance instanceof IDialect)) {
                throw new RuntimeException("Mybatis-plus方言实现类配置错误！");
            }

            paginationInnerInterceptor.setDialect((IDialect)dialectInstance);
        }

        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return (configuration) -> {
            configuration.setUseDeprecatedExecutor(false);
        };
    }

    @Bean({"transactionManager"})
    @Primary
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
